﻿Namespace Global.System

    Public Class CompareableObject(Of T)
        Implements IComparable(Of CompareableObject(Of T))

        Public Property Value() As T
        Public Property Comparer() As Comparison(Of T)

        Public Sub New()
            Me.Value = Nothing
            Me.Comparer = AddressOf System.Collections.Generic.Comparer(Of T).Default.Compare
        End Sub

        Public Sub New(ByVal obj As T)
            Me.Value = obj
            Me.Comparer = AddressOf System.Collections.Generic.Comparer(Of T).Default.Compare
        End Sub

        Public Sub New(ByVal obj As T, ByVal compFun As Comparison(Of T))
            Me.Value = obj
            Me.Comparer = compFun
        End Sub

        Public Shared Widening Operator CType(ByVal obj As T) As CompareableObject(Of T)
            Return New CompareableObject(Of T)(obj)
        End Operator

        Public Function CompareTo(other As CompareableObject(Of T)) As Integer Implements IComparable(Of CompareableObject(Of T)).CompareTo
            Return Me.Comparer(Me.Value, other.Value)
        End Function

    End Class

End Namespace